/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import React, { useState, useEffect, useRef } from 'react';
import { BreakpointManager } from '@hadss/web_adaptive_layout_ui';
import { TabBarContainerReact, TabBarContentReact, TabBarReact, TabBarItemReact, MultiGridReact } from '@hadss/web_adaptive_layout_react';
import CustomTab from '../CustomTab/CustomTab';
import { Swiper, SwiperSlide } from 'swiper/react';
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
import { videoGridData, videoImages, entrances, tabBarData } from '../../../mocks/mockData';
import './VideoHome.css';
import { AvoidArea, AvoidAreaInfo, AvoidAreaType } from '../../../common/windowCommon';

const VideoHome = () => {
  const breakpointManager = new BreakpointManager();
  const [slidesPerViewCount, setSlidesPerViewCount] = useState(1);
  const [entrancesPadding, setEntrancesPadding] = useState('38px');
  const [topAvoidArea, setTopAvoidArea] = useState(0);
  const [bottomAvoidArea, setBottomAvoidArea] = useState(0);
  const columnsTemplate = {
    "sm": '1fr 1fr',
    "md": '1fr 1fr 1fr',
    "lg": '1fr 1fr 1fr 1fr'
  };
  const [barHeight, setBarHeight] = useState({
    "sm": '56px',
    "md": '48px',
    "lg": '100%'
  });
  const barWidth = {
    "md": '100%',
    "lg": '96px'
  };

  const handleAvoidAreaChange = (event: any) => {
    const data = event.detail as AvoidAreaInfo;
    const areaType = data.type;
    if (areaType === AvoidAreaType.TYPE_SYSTEM) {
      const topAvoidArea = (window as any).webClass?.pxToVp(data.area.topRect.height);
      setTopAvoidArea(topAvoidArea);
    }
    if (areaType === AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {
      const bottomAvoidArea = (window as any).webClass?.pxToVp(data.area.bottomRect.height);
      setBottomAvoidArea(bottomAvoidArea);
      setBarHeight({
        "sm": `${56 + bottomAvoidArea}px`,
        "md": `${48 + bottomAvoidArea}px`,
        "lg": '100%'
      });
    }
  };

  const setAvoidValue = () => {
    if ((window as any).webClass) {
      const webClass = (window as any).webClass;
      const topArea = webClass.getAvoidArea(AvoidAreaType.TYPE_SYSTEM) as AvoidArea;
      const bottomArea = webClass.getAvoidArea(AvoidAreaType.TYPE_NAVIGATION_INDICATOR) as AvoidArea;
      const topAvoidArea = webClass.pxToVp(topArea.topRect.height);
      const bottomAvoidArea = webClass.pxToVp(bottomArea.bottomRect.height)
      setTopAvoidArea(topAvoidArea);
      setBottomAvoidArea(bottomAvoidArea);
      setBarHeight({
        "sm": `${56 + bottomAvoidArea}px`,
        "md": `${48 + bottomAvoidArea}px`,
        "lg": '100%'
      });
    }
  };

  useEffect(() => {
    const unsubscribe = breakpointManager.subscribeToBreakpoint(() => {
      setSlidesPerViewCount(breakpointManager.useBreakpointValue({
        sm: 1,
        md: 2,
        lg: 2,
        xl: 3
      }));
      setEntrancesPadding(breakpointManager.useBreakpointValue({
        sm: '38px',
        md: '65px',
        lg: '100px'
      }));
    });
    setAvoidValue();
    window.addEventListener("avoidAreaChange", handleAvoidAreaChange);

    return () => {
      if (unsubscribe) {
        unsubscribe();
      }
      window.removeEventListener("avoidAreaChange", handleAvoidAreaChange);
    };
  }, []);

  return (
    <div className="container">
      <TabBarContainerReact>
        <TabBarContentReact>
          <div className="main-content" style={{ paddingTop: `${topAvoidArea}px` }}>
            <CustomTab />
            <Swiper
              loop
              autoplay={false}
              spaceBetween={30}
              effect='slide'
              speed={500}
              resistance={true}
              resistanceRatio={0.85}
              slidesPerView={slidesPerViewCount}
            >
              {videoImages.map((image, index) => (
                <SwiperSlide key={index}>
                  <img src={image.uri} className="carousel-image" />
                </SwiperSlide>
              ))}
              <div className="swiper-pagination" slot="pagination" />
            </Swiper>

            <div className="entrances-centent">
              <Swiper
                direction={'horizontal'}
                autoplay={false}
                loop={false}
                freeMode={true}
                grabCursor={true}
                resistanceRatio={0}
                slidesPerView="auto"
              >
                {entrances.map((image, index) => (
                  <SwiperSlide key={index} className="swiper-slide">
                    <div className="entrances" style={{ paddingRight: entrancesPadding }}>
                      <img src={image.uri} className="entrances-image" />
                      <div className="entrances-text">{image.name}</div>
                    </div>
                  </SwiperSlide>
                ))}
              </Swiper>
            </div>

            <MultiGridReact columnsTemplate={columnsTemplate} gridColumnGap="20px" gridRowGap="12px">
              {videoGridData.map((item) => (
                <div key={item.id}>
                  <div className="item-container">
                    <img src={require('../../../assets/videoList/images/image1.png')} style={{ width: '100%' }} />
                    <div className="text-overlay">
                      <div className="top-left">正片</div>
                      <div className="bottom-right">9.0</div>
                    </div>
                  </div>
                  <div className="item-content">
                    <div className="common-text item-title">这是超人的水准</div>
                    <div className="common-text item-subtitle">中秋节端午节重阳节泼水节节日快乐节日快乐</div>
                  </div>
                </div>
              ))}
            </MultiGridReact>
          </div>
        </TabBarContentReact>
        <TabBarReact bgColor={'#F1F3F5'} height={barHeight} width={barWidth}>
          {tabBarData.map((title, index) => (
            <TabBarItemReact>
              <div className="tab-item" style={{ marginBottom: `${bottomAvoidArea}px` }}>
                <img className="bottom-img" src={title.uri} />
                <span className={index === 0 ? 'bottom-title-active' : 'bottom-title'}>{title.name}</span>
              </div>
            </TabBarItemReact>
          ))}
        </TabBarReact>
      </TabBarContainerReact>
    </div >
  );
};

export default VideoHome;